/* $Id: calculator.js 1928 2006-10-17 06:10:56Z weberliu $ */

var Calculator = Object();

Calculator.result = 0;
Calculator.current = '';
Calculator.values = 0;
Calculator.handle = '';

Calculator.elem = function(){ return document.getElementById('calculator'); };
Calculator.output = function() { return document.getElementById('calculatorOutput'); }

Calculator.show = function()
{
  Calculator.elem().style.visibility = 'visible';
}

Calculator.close = function()
{
  Calculator.elem().style.visibility = 'hidden';
}

Calculator.input = function(n)
{
  var output = Calculator.output();

  Calculator.current += "" + n;

  output.innerHTML = Calculator.current;
}

Calculator.calculate = function(p)
{
  if (Calculator.handle != '' && Calculator.values != '' && Calculator.current != '')
  {
    //alert(Calculator.values + Calculator.handle + Calculator.current);

    try
    {
      Calculator.values = eval(Calculator.values + Calculator.handle + Calculator.current);    	
    }
    catch (e)
    {
      alert(e);
    }
  }
  else
  {
    Calculator.values = Calculator.current;
  }

  if (p == '=')
  {
    Calculator.output().innerHTML = Calculator.values == '' ? '0' : Calculator.values;
    Calculator.current = Calculator.values;
    Calculator.handle = '';
  }
  else
  {
    Calculator.handle = p;
  }
  
  Calculator.current = '';
}